#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "scene.h"
#include "ctrlboard.h"
#include "mg_Lcd_Driver.h"
#include "mg_Device.h"
#include "mg_Rs485Protocol.h"
/* widgets:
Layer_Config_SetLight
Background_TGD
Container_TGD_On
Container_TGD_Text
Text_TGD_Baifenhao
Text_TGD_Liangdu
Container_TGD_Button
PopupButton_ReturnMenu
Meter_TGD_Xuanzhuan
Icon_TGD_Pointer
CircleProgressBar_TGD_Light
Text_TGD_PercentNum
Icon_TGD_Jingdu
*/
static ITUBackground* Background_ConfigBacklight;
static ITUMeter* Meter_ConfigBacklight_Xuanzhuan;
static ITUText* Text_ConfigBacklight_PercentNum;
static ITUText* Text_ConfigBacklight_Baifenhao;
static ITUText* Text_ConfigBacklight_Liangdu;
uint16_t BacklightPwm = 101;
bool ConfigSetLightOnEnter(ITUWidget* widget, char* param)
{
	char TextString[20];
	if (!Background_ConfigBacklight)
	{
		Background_ConfigBacklight = ituSceneFindWidget(&theScene, "Background_ConfigBacklight");
		assert(Background_ConfigBacklight);
		Meter_ConfigBacklight_Xuanzhuan = ituSceneFindWidget(&theScene, "Meter_ConfigBacklight_Xuanzhuan");
		assert(Meter_ConfigBacklight_Xuanzhuan);
		Text_ConfigBacklight_PercentNum = ituSceneFindWidget(&theScene, "Text_ConfigBacklight_PercentNum");
		assert(Text_ConfigBacklight_PercentNum);
		Text_ConfigBacklight_Baifenhao = ituSceneFindWidget(&theScene, "Text_ConfigBacklight_Baifenhao");
		assert(Text_ConfigBacklight_Baifenhao);
		Text_ConfigBacklight_Liangdu = ituSceneFindWidget(&theScene, "Text_ConfigBacklight_Liangdu");
		assert(Text_ConfigBacklight_Liangdu);
	}

	MessageFlag[MSG_GET_DEVICE_STATE_EN] = OFF;//关闭10s自动获取状态
	if (BacklightPwm>100)
		BacklightPwm = Sys_Config.byte.LightPercent;
	ituMeterSetValue(Meter_ConfigBacklight_Xuanzhuan, BacklightPwm - 30);
	itoa(BacklightPwm, TextString, 10);//转换成字符串
	ituTextSetString(Text_ConfigBacklight_PercentNum, TextString);//显示字符

	//对标题进行语言转换
	if (Sys.Language == MG_LANGUAGE_CHS || Sys.Language == MG_LANGUAGE_ENG)
	{
		Text_ConfigBacklight_Liangdu->fontIndex = 0;
		Text_ConfigBacklight_Liangdu->boldSize = 1;
		ituTextSetFontSize(Text_ConfigBacklight_Liangdu, 18);
	}
	else if (Sys.Language == MG_LANGUAGE_ZHTW)
	{
		Text_ConfigBacklight_Liangdu->fontIndex = 8;
		Text_ConfigBacklight_Liangdu->boldSize = 0;
		ituTextSetFontSize(Text_ConfigBacklight_Liangdu, 16);
	}
	//对“%”坐标判断
	if (BacklightPwm >= 100)
		ituWidgetSetX(Text_ConfigBacklight_Baifenhao, 141);
	else
		ituWidgetSetX(Text_ConfigBacklight_Baifenhao, 123);
	Set_Rotate_Block(30);//旋转步进
    return true;
}

bool ConfigSetLightOnLeave(ITUWidget* widget, char* param)
{

	MessageFlag[MSG_GET_DEVICE_STATE_EN] = ON;//打开10s自动获取状态
    return true;
}

bool ConfigSetLightOnTimer(ITUWidget* widget, char* param)
{
	static int MeterValue = 0, MeterValue_L = 0;
	static uint32_t SendEndTime = Rotate_SendCmd_Time;
	MeterValue = Meter_ConfigBacklight_Xuanzhuan->value;
	//用户手势操作
	if (Action_Cmd.User_Action == Left_Slide || Action_Cmd.User_Action == Right_Slide)
	{
		GotoConfigLayer();//返回设置界面
		Clear_User_Action();
	}
	if (Action_Cmd.User_Action == Clockwise_Rotation)//有用户操作
	{
		MeterValue += Action_Cmd.Action_Times;
		if (MeterValue > 100)MeterValue = 100;
		ituMeterSetValue(Meter_ConfigBacklight_Xuanzhuan, MeterValue);//设置Meter百分比值
		Clear_User_Action();
	}
	else if (Action_Cmd.User_Action == Anticlockwise_Rotation)
	{
		MeterValue -= Action_Cmd.Action_Times;
		if (MeterValue < 0)MeterValue = 0;
		ituMeterSetValue(Meter_ConfigBacklight_Xuanzhuan, MeterValue);//设置Meter百分比值
		Clear_User_Action();
	}
	//结束操作设备状态执行
	if (MeterValue == MeterValue_L)
	{
		if (SendEndTime <= Rotate_SendCmd_Time)
			SendEndTime++;
		//停止操作0.6s后发送执行报文
		if (SendEndTime == Rotate_SendCmd_Time)
		{
			MessageFlag[MSG_SAVE_SYS_CONFIG] = ON;
		}
	}
	else
	{
		SendEndTime = 0;
	}

	//对“%”坐标判断
	if (BacklightPwm >= 100)
		ituWidgetSetX(Text_ConfigBacklight_Baifenhao, 141);
	else
		ituWidgetSetX(Text_ConfigBacklight_Baifenhao, 123);

	MeterValue_L = MeterValue;
    return true;
}

bool Set_TFT_BackLight(ITUWidget* widget, char* param)
{
	char TextString[20];
	BacklightPwm = Meter_ConfigBacklight_Xuanzhuan->value+30;
	itoa(BacklightPwm, TextString, 10);//转换成字符串
	ituTextSetString(Text_ConfigBacklight_PercentNum, TextString);//显示字符
	Sys_Config.byte.LightPercent = BacklightPwm;
	BackLight_Duty_Set(BacklightPwm);
    return true;
}

